#include "syscall.h"

/*
** type:0=>KillTask
**      1=>MutexCallHandler
** cmd: 0=>SysCreateMutex
**      1=>SysEnterCritical
**      2=>SysExitCritical
**      3=>SysDestroyMutex
*/
#define SysCall(type, cmd, param1, param2)  asm volatile (                      \
                                                "movl $" #type ", %%eax \n"     \
                                                "movl $" #cmd  ", %%ebx \n"     \
                                                "movl %0,         %%ecx \n"     \
                                                "movl %1,         %%edx \n"     \
                                                "int   $0x80            \n"     \
                                                :                               \
                                                : "r"(param1), "r"(param2)      \
                                                : "eax", "ebx", "ecx", "edx"    \
                                            ); 

void Exit()
{
    // type, cmd, param1, param2
    SysCall(0, 0, 0, 0);
}

uint CreateMutex(uint type)
{
    volatile uint ret = 0;

    // type, cmd, param1, param2
    SysCall(1, 0, &ret, type);

    return ret;
}

void EnterCritical(uint mutex)
{
    volatile uint wait = 0;

    do {
        // type, cmd, param1, param2
        SysCall(1, 1, mutex, &wait);

    } while(wait);
}

void ExitCritical(uint mutex)
{
    // type, cmd, param1, param2
    SysCall(1, 2, mutex, 0);
}

uint DestroyMutex(uint mutex)
{
    uint ret = 0;

    // type, cmd, param1, param2
    SysCall(1, 3, mutex, &ret);

    return ret;
}
